package holdtime.xlxc.tools.util;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import holdtime.xlxc.R;

public class ActionBarUtil extends AppCompatActivity {

    private String actionBarTitle;
    private boolean homeAsUpEnabled;
    private Drawable indicator;

    public String getActionBarTitle() {
        return actionBarTitle;
    }

    public void setActionBarTitle(String actionBarTitle) {
        this.actionBarTitle = actionBarTitle;
    }

    public boolean isHomeAsUpEnabled() {
        return homeAsUpEnabled;
    }

    public void setHomeAsUpEnabled(boolean homeAsUpEnabled) {
        this.homeAsUpEnabled = homeAsUpEnabled;
    }

    public Drawable getIndicator() {
        return indicator;
    }

    public void setIndicator(Drawable indicator) {
        this.indicator = indicator;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        assert getSupportActionBar() != null;
        getSupportActionBar().setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.nav_white, null));//line under the action bar
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER);
//        View customView = getLayoutInflater().inflate(R.layout.actionbar_layout, null);
        View customView = getLayoutInflater().inflate(R.layout.actionbar_layout, new LinearLayout(this), false);

        TextView textView = (TextView) customView.findViewById(R.id.actionbar_textview);
        textView.setText(actionBarTitle);

        getSupportActionBar().setCustomView(customView, params);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnabled);
        getSupportActionBar().setHomeAsUpIndicator(indicator);
        getSupportActionBar().setElevation(0);//5.0以上移除阴影
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
